﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using mscook.core.domain;
using NHibernate.Linq;
namespace mscook.core.repositories
{
    public class ChefRepository : Repository<Chef>, IChefRepository
    {
        public IList<Chef> SelectPageList(int start, int limit, out long total,string chefName)
        {
            var query = (from x in this.Session.Query<Chef>() select x)
               .Where(o => o.IsDelete == false
                   &&(string.IsNullOrEmpty(chefName)||o.Name.Contains(chefName))
               ).OrderByDescending(o => o.EntityIndex)
               .OrderByDescending(o => o.CreateTime);
            total = query.LongCount();

            if (start > total)
            {
                int tempStart = Int32.Parse(total.ToString()) - limit;
                return query.Skip(tempStart).Take(limit).ToList<Chef>();
            }
            return query.Skip(start).Take(limit).ToList<Chef>();
        }

        public IList<Chef> GetByKeyword(string keyword, int start, int limit, out long total)
        {
            var query = (from x in this.Session.Query<Chef>() select x)
           .Where(o => o.IsDelete == false
               && (o.Name.Contains(keyword) || o.Summary.Contains(keyword))).OrderByDescending(o => o.EntityIndex);
            total = query.Count();
            return query.Skip(start).Take(limit).ToList();
        }
    }
}
